#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
set builtin 类型的测试

测试 Python set 的标准功能和特性
"""

import copy
from typing import Any
from gtest import test, EXPECT_EQ, EXPECT_TRUE, EXPECT_FALSE


# ============================================================================
# 基础操作测试
# ============================================================================


@test("SetBuiltin", "BasicCreation")
def test_basic_creation():
    """测试集合创建"""
    s: set[int] = {1, 2, 3}
    EXPECT_EQ(len(s), 3)


@test("SetBuiltin", "EmptySet")
def test_empty_set():
    """测试空集合"""
    # 强类型空 set
    empty1: set[int] = set()
    EXPECT_EQ(len(empty1), 0)
    EXPECT_FALSE(bool(empty1))

    # 弱类型空 set
    empty2: set = set()
    EXPECT_EQ(len(empty2), 0)


@test("SetBuiltin", "Add")
def test_add():
    """测试添加元素"""
    s: set[int] = {1, 2}
    s.add(3)
    EXPECT_EQ(len(s), 3)
    EXPECT_TRUE(3 in s)

    # 添加重复元素应该被忽略
    s.add(2)
    EXPECT_EQ(len(s), 3)


@test("SetBuiltin", "Remove")
def test_remove():
    """测试移除元素"""
    s: set[int] = {1, 2, 3}
    s.remove(2)
    EXPECT_EQ(len(s), 2)
    EXPECT_FALSE(2 in s)


@test("SetBuiltin", "Discard")
def test_discard():
    """测试 discard 方法"""
    s: set[int] = {1, 2, 3}
    s.discard(2)
    EXPECT_EQ(len(s), 2)

    # discard 不存在的元素不会报错
    s.discard(999)
    EXPECT_EQ(len(s), 2)


@test("SetBuiltin", "Pop")
def test_pop():
    """测试 pop 方法"""
    s: set[int] = {1, 2, 3}
    item: int = s.pop()
    EXPECT_EQ(len(s), 2)
    # 弹出的元素应该是原集合中的元素
    EXPECT_TRUE(item == 1 or item == 2 or item == 3)


@test("SetBuiltin", "Clear")
def test_clear():
    """测试清空集合"""
    s: set[int] = {1, 2, 3}
    s.clear()
    EXPECT_EQ(len(s), 0)
    EXPECT_FALSE(bool(s))


@test("SetBuiltin", "Contains")
def test_contains():
    """测试成员检测"""
    s: set[int] = {1, 2, 3}
    EXPECT_TRUE(1 in s)
    EXPECT_TRUE(2 in s)
    EXPECT_TRUE(3 in s)
    EXPECT_FALSE(4 in s)


@test("SetBuiltin", "Length")
def test_length():
    """测试长度操作"""
    s: set[int] = {1, 2, 3, 4, 5}
    EXPECT_EQ(len(s), 5)

    s.remove(1)
    EXPECT_EQ(len(s), 4)


# ============================================================================
# 集合运算测试
# ============================================================================


@test("SetBuiltin", "Union")
def test_union():
    """测试并集"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {2, 3, 4}
    s3: set[int] = s1.union(s2)
    EXPECT_EQ(len(s3), 4)
    EXPECT_TRUE(1 in s3)
    EXPECT_TRUE(4 in s3)


@test("SetBuiltin", "UnionOperator")
def test_union_operator():
    """测试并集运算符 |"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {2, 3, 4}
    s3: set[int] = s1 | s2
    EXPECT_EQ(len(s3), 4)


@test("SetBuiltin", "Intersection")
def test_intersection():
    """测试交集"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {2, 3, 4}
    s3: set[int] = s1.intersection(s2)
    EXPECT_EQ(len(s3), 2)
    EXPECT_TRUE(2 in s3)
    EXPECT_TRUE(3 in s3)


@test("SetBuiltin", "IntersectionOperator")
def test_intersection_operator():
    """测试交集运算符 &"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {2, 3, 4}
    s3: set[int] = s1 & s2
    EXPECT_EQ(len(s3), 2)


@test("SetBuiltin", "Difference")
def test_difference():
    """测试差集"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {2, 3, 4}
    s3: set[int] = s1.difference(s2)
    EXPECT_EQ(len(s3), 1)
    EXPECT_TRUE(1 in s3)


@test("SetBuiltin", "DifferenceOperator")
def test_difference_operator():
    """测试差集运算符 -"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {2, 3, 4}
    s3: set[int] = s1 - s2
    EXPECT_EQ(len(s3), 1)


@test("SetBuiltin", "SymmetricDifference")
def test_symmetric_difference():
    """测试对称差集"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {2, 3, 4}
    s3: set[int] = s1.symmetric_difference(s2)
    EXPECT_EQ(len(s3), 2)
    EXPECT_TRUE(1 in s3)
    EXPECT_TRUE(4 in s3)


@test("SetBuiltin", "SymmetricDifferenceOperator")
def test_symmetric_difference_operator():
    """测试对称差集运算符 ^"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {2, 3, 4}
    s3: set[int] = s1 ^ s2
    EXPECT_EQ(len(s3), 2)


# ============================================================================
# 原地修改操作测试
# ============================================================================


@test("SetBuiltin", "Update")
def test_update():
    """测试 update 方法（原地并集）"""
    s1: set[int] = {1, 2}
    s2: set[int] = {2, 3, 4}
    s1.update(s2)
    EXPECT_EQ(len(s1), 4)
    EXPECT_TRUE(4 in s1)


@test("SetBuiltin", "InplaceUnion")
def test_inplace_union():
    """测试原地并集运算符 |="""
    s1: set[int] = {1, 2}
    s2: set[int] = {2, 3, 4}
    s1 |= s2
    EXPECT_EQ(len(s1), 4)


@test("SetBuiltin", "IntersectionUpdate")
def test_intersection_update():
    """测试 intersection_update 方法"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {2, 3, 4}
    s1.intersection_update(s2)
    EXPECT_EQ(len(s1), 2)
    EXPECT_TRUE(2 in s1)
    EXPECT_TRUE(3 in s1)


@test("SetBuiltin", "InplaceIntersection")
def test_inplace_intersection():
    """测试原地交集运算符 &="""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {2, 3, 4}
    s1 &= s2
    EXPECT_EQ(len(s1), 2)


@test("SetBuiltin", "DifferenceUpdate")
def test_difference_update():
    """测试 difference_update 方法"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {2, 3, 4}
    s1.difference_update(s2)
    EXPECT_EQ(len(s1), 1)
    EXPECT_TRUE(1 in s1)


@test("SetBuiltin", "InplaceDifference")
def test_inplace_difference():
    """测试原地差集运算符 -="""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {2, 3, 4}
    s1 -= s2
    EXPECT_EQ(len(s1), 1)


@test("SetBuiltin", "SymmetricDifferenceUpdate")
def test_symmetric_difference_update():
    """测试 symmetric_difference_update 方法"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {2, 3, 4}
    s1.symmetric_difference_update(s2)
    EXPECT_EQ(len(s1), 2)
    EXPECT_TRUE(1 in s1)
    EXPECT_TRUE(4 in s1)


@test("SetBuiltin", "InplaceSymmetricDifference")
def test_inplace_symmetric_difference():
    """测试原地对称差集运算符 ^="""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {2, 3, 4}
    s1 ^= s2
    EXPECT_EQ(len(s1), 2)


# ============================================================================
# 集合关系测试
# ============================================================================


@test("SetBuiltin", "Issubset")
def test_issubset():
    """测试子集判断"""
    s1: set[int] = {1, 2}
    s2: set[int] = {1, 2, 3}
    s3: set[int] = {1, 2}

    EXPECT_TRUE(s1.issubset(s2))
    EXPECT_TRUE(s1.issubset(s3))
    EXPECT_FALSE(s2.issubset(s1))


@test("SetBuiltin", "SubsetOperator")
def test_subset_operator():
    """测试子集运算符 <="""
    s1: set[int] = {1, 2}
    s2: set[int] = {1, 2, 3}
    EXPECT_TRUE(s1 <= s2)


@test("SetBuiltin", "ProperSubset")
def test_proper_subset():
    """测试真子集运算符 <"""
    s1: set[int] = {1, 2}
    s2: set[int] = {1, 2, 3}
    s3: set[int] = {1, 2}

    EXPECT_TRUE(s1 < s2)
    EXPECT_FALSE(s1 < s3)  # 相等不是真子集


@test("SetBuiltin", "Issuperset")
def test_issuperset():
    """测试超集判断"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {1, 2}

    EXPECT_TRUE(s1.issuperset(s2))
    EXPECT_FALSE(s2.issuperset(s1))


@test("SetBuiltin", "SupersetOperator")
def test_superset_operator():
    """测试超集运算符 >="""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {1, 2}
    EXPECT_TRUE(s1 >= s2)


@test("SetBuiltin", "ProperSuperset")
def test_proper_superset():
    """测试真超集运算符 >"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {1, 2}
    s3: set[int] = {1, 2, 3}

    EXPECT_TRUE(s1 > s2)
    EXPECT_FALSE(s1 > s3)  # 相等不是真超集


@test("SetBuiltin", "Isdisjoint")
def test_isdisjoint():
    """测试是否不相交"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {4, 5, 6}
    s3: set[int] = {3, 4, 5}

    EXPECT_TRUE(s1.isdisjoint(s2))
    EXPECT_FALSE(s1.isdisjoint(s3))


# ============================================================================
# 拷贝和相等测试
# ============================================================================


@test("SetBuiltin", "Copy")
def test_copy():
    """测试 copy 方法"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = s1.copy()
    EXPECT_EQ(len(s2), 3)

    # 修改副本不影响原集合
    s2.add(4)
    EXPECT_EQ(len(s1), 3)
    EXPECT_EQ(len(s2), 4)


@test("SetBuiltin", "ShallowCopy")
def test_shallow_copy():
    """测试浅拷贝"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = copy.copy(s1)

    s2.add(4)
    EXPECT_EQ(len(s1), 3)
    EXPECT_EQ(len(s2), 4)


@test("SetBuiltin", "DeepCopy")
def test_deep_copy():
    """测试深拷贝"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = copy.deepcopy(s1)

    s2.add(4)
    EXPECT_EQ(len(s1), 3)
    EXPECT_EQ(len(s2), 4)


@test("SetBuiltin", "Equality")
def test_equality():
    """测试相等比较"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = {1, 2, 3}
    s3: set[int] = {1, 2, 4}

    EXPECT_TRUE(s1 == s2)
    EXPECT_FALSE(s1 == s3)
    EXPECT_FALSE(s1 != s2)
    EXPECT_TRUE(s1 != s3)


# ============================================================================
# 类型转换和构造测试
# ============================================================================


@test("SetBuiltin", "FromList")
def test_from_list():
    """测试从列表创建 set（自动去重）"""
    numbers: list[int] = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5]
    s: set[int] = set(numbers)

    EXPECT_EQ(len(s), 5)
    EXPECT_TRUE(1 in s)
    EXPECT_TRUE(5 in s)


@test("SetBuiltin", "ToList")
def test_to_list():
    """测试转换为列表"""
    s: set[int] = {3, 1, 4, 5, 9, 2, 6}
    lst: list[int] = list(s)

    EXPECT_EQ(len(lst), 7)
    EXPECT_TRUE(1 in lst)
    EXPECT_TRUE(9 in lst)


# ============================================================================
# 不同元素类型测试
# ============================================================================


@test("SetBuiltin", "StringSet")
def test_string_set():
    """测试字符串集合"""
    s: set[str] = {"apple", "banana", "orange"}
    EXPECT_EQ(len(s), 3)
    EXPECT_TRUE("apple" in s)
    EXPECT_FALSE("grape" in s)

    s.add("grape")
    EXPECT_TRUE("grape" in s)


@test("SetBuiltin", "StringSetOperations")
def test_string_set_operations():
    """测试字符串集合运算"""
    s1: set[str] = {"a", "b", "c"}
    s2: set[str] = {"b", "c", "d"}

    union_result: set[str] = s1 | s2
    EXPECT_EQ(len(union_result), 4)

    intersection_result: set[str] = s1 & s2
    EXPECT_EQ(len(intersection_result), 2)
    EXPECT_TRUE("b" in intersection_result)
    EXPECT_TRUE("c" in intersection_result)


@test("SetBuiltin", "MixedTypeSet")
def test_mixed_type_set():
    """测试弱类型 set（混合类型）"""
    s: set = {1, "hello", 3.14}
    EXPECT_EQ(len(s), 3)

    one: int = 1
    EXPECT_TRUE(one in s)
    EXPECT_TRUE("hello" in s)


# ============================================================================
# 集合推导式测试
# ============================================================================


@test("SetBuiltin", "BasicComprehension")
def test_basic_comprehension():
    """测试基本集合推导式"""
    numbers: list[int] = [1, 2, 3, 4, 5]
    doubled: set[int] = {x * 2 for x in numbers}

    EXPECT_EQ(len(doubled), 5)
    EXPECT_TRUE(2 in doubled)
    EXPECT_TRUE(10 in doubled)


@test("SetBuiltin", "ComprehensionWithCondition")
def test_comprehension_with_condition():
    """测试带条件的集合推导式"""
    numbers: list[int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    # 筛选偶数
    evens: set[int] = {x for x in numbers if x % 2 == 0}
    EXPECT_EQ(len(evens), 5)
    EXPECT_TRUE(2 in evens)
    EXPECT_TRUE(10 in evens)

    # 筛选并转换
    even_squares: set[int] = {x * x for x in numbers if x % 2 == 0}
    EXPECT_EQ(len(even_squares), 5)
    EXPECT_TRUE(4 in even_squares)


@test("SetBuiltin", "ComprehensionDedup")
def test_comprehension_dedup():
    """测试集合推导式的自动去重"""
    numbers: list[int] = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
    unique: set[int] = {x for x in numbers}

    EXPECT_EQ(len(unique), 4)
    EXPECT_TRUE(1 in unique)

    numbers_1 = list[int](unique)
    numbers_1.sort()
    expected = [1, 2, 3, 4]
    EXPECT_EQ(numbers_1, expected)


# ============================================================================
# 迭代测试
# ============================================================================


@test("SetBuiltin", "ForLoop")
def test_for_loop():
    """测试 for 循环遍历"""
    s: set[int] = {1, 2, 3, 4, 5}

    total = 0
    for num in s:
        total = total + num
    EXPECT_EQ(total, 15)

    count = 0
    for item in s:
        count = count + 1
    EXPECT_EQ(count, 5)


# ============================================================================
# 内置函数测试
# ============================================================================


@test("SetBuiltin", "MinMax")
def test_min_max():
    """测试 min/max 函数"""
    numbers: set[int] = {3, 1, 4, 5, 9, 2, 6}
    EXPECT_EQ(min(numbers), 1)
    EXPECT_EQ(max(numbers), 9)


@test("SetBuiltin", "Sum")
def test_sum():
    """测试 sum 函数"""
    numbers: set[int] = {1, 2, 3, 4, 5}
    EXPECT_EQ(sum(numbers), 15)

    # 带起始值
    EXPECT_EQ(sum(numbers, 10), 25)


@test("SetBuiltin", "Sorted")
def test_sorted():
    """测试 sorted 函数"""
    s: set[int] = {3, 1, 4, 5, 9, 2, 6}

    # 升序
    sorted_list: list[int] = sorted(s)
    EXPECT_EQ(len(sorted_list), 7)
    EXPECT_EQ(sorted_list[0], 1)
    EXPECT_EQ(sorted_list[-1], 9)

    # 降序
    sorted_desc: list[int] = sorted(s, reverse=True)
    EXPECT_EQ(sorted_desc[0], 9)
    EXPECT_EQ(sorted_desc[-1], 1)


@test("SetBuiltin", "AnyAll")
def test_any_all():
    """测试 any/all 函数"""
    bool_set1: set[bool] = {True, False}
    bool_set2: set[bool] = {False}
    bool_set3: set[bool] = {True}

    EXPECT_TRUE(any(bool_set1))
    EXPECT_FALSE(any(bool_set2))
    EXPECT_TRUE(any(bool_set3))

    EXPECT_FALSE(all(bool_set1))
    EXPECT_FALSE(all(bool_set2))
    EXPECT_TRUE(all(bool_set3))


@test("SetBuiltin", "Len")
def test_len():
    """测试 len 函数"""
    s: set[int] = {1, 2, 3, 4, 5}
    EXPECT_EQ(len(s), 5)


@test("SetBuiltin", "Bool")
def test_bool():
    """测试 bool 函数"""
    non_empty: set[int] = {1}
    empty: set[int] = set()

    EXPECT_TRUE(bool(non_empty))
    EXPECT_FALSE(bool(empty))


# ============================================================================
# Any 类型测试
# ============================================================================


@test("SetBuiltin", "SetToAny")
def test_set_to_any():
    """测试 set 赋值给 Any 类型"""
    v: Any = {1, 2, 3}
    data: dict = {"value": v}
    EXPECT_TRUE("value" in data)


@test("SetBuiltin", "SetFromAny")
def test_set_from_any():
    """测试从 Any 类型取回 set"""
    v: Any = {1, 2, 3}
    s: set = v
    EXPECT_EQ(len(s), 3)
    EXPECT_TRUE(1 in s)


@test("SetBuiltin", "SetInDict")
def test_set_in_dict():
    """测试 set 在 dict 中的存储"""
    data: dict = {
        "int_set": {1, 2, 3},
        "str_set": {"a", "b", "c"},
        "mixed_set": {1, "hello", 3.14},
    }

    EXPECT_TRUE("int_set" in data)
    EXPECT_TRUE("str_set" in data)
    EXPECT_TRUE("mixed_set" in data)


@test("SetBuiltin", "SetAndListInDict")
def test_set_and_list_in_dict():
    """测试 set 和 list 在 dict 中共存"""
    data: dict = {"my_set": {1, 2, 3}, "my_list": [1, 2, 3]}

    EXPECT_TRUE("my_set" in data)
    EXPECT_TRUE("my_list" in data)


# ============================================================================
# 组合和边界测试
# ============================================================================


@test("SetBuiltin", "EmptySetOperations")
def test_empty_set_operations():
    """测试空集合运算"""
    # 创建空集合
    empty: set[int] = set()
    non_empty: set[int] = {1, 2, 3}

    # 空集合的并集
    result1: set[int] = empty | non_empty
    EXPECT_EQ(len(result1), 3)

    # 空集合的交集
    result2: set[int] = empty & non_empty
    EXPECT_EQ(len(result2), 0)

    # 空集合的差集
    result3: set[int] = non_empty - empty
    EXPECT_EQ(len(result3), 3)


@test("SetBuiltin", "ChainedOperations")
def test_chained_operations():
    """测试链式操作"""
    s: set[int] = {1}
    s.add(2)
    s.add(3)
    s.add(4)
    EXPECT_EQ(len(s), 4)

    s.remove(2)
    s.discard(3)
    EXPECT_EQ(len(s), 2)


@test("SetBuiltin", "CombinedOperations")
def test_combined_operations():
    """测试组合运算"""
    s1: set[int] = {1, 2, 3, 4, 5}
    s2: set[int] = {3, 4, 5, 6, 7}
    s3: set[int] = {5, 6, 7, 8, 9}

    # (s1 | s2) & s3
    temp: set[int] = s1 | s2
    result: set[int] = temp & s3

    EXPECT_EQ(len(result), 3)
    EXPECT_TRUE(5 in result)
    EXPECT_TRUE(6 in result)
    EXPECT_TRUE(7 in result)


@test("SetBuiltin", "SubsetTransitivity")
def test_subset_transitivity():
    """测试子集关系的传递性"""
    small: set[int] = {1, 2}
    medium: set[int] = {1, 2, 3}
    large: set[int] = {1, 2, 3, 4, 5}

    EXPECT_TRUE(small < medium)
    EXPECT_TRUE(medium < large)
    EXPECT_TRUE(small < large)


@test("SetBuiltin", "LargeSet")
def test_large_set():
    """测试大型集合"""
    s: set[int] = set()

    for i in range(100):
        s.add(i)

    EXPECT_EQ(len(s), 100)
    EXPECT_TRUE(0 in s)
    EXPECT_TRUE(99 in s)
    EXPECT_FALSE(100 in s)


# ============================================================================
# 迭代函数测试
# ============================================================================


@test("SetBuiltin", "EnumerateFunction")
def test_enumerate_function():
    """测试 enumerate 函数"""
    s: set[int] = {10, 20, 30}
    indexed: list = []

    for pair in enumerate(s):
        indexed.append(pair)

    EXPECT_EQ(len(indexed), 3)


@test("SetBuiltin", "EnumerateWithStart")
def test_enumerate_with_start():
    """测试 enumerate 函数带起始值"""
    s: set[int] = {100, 200, 300}
    indexed: list = []

    for pair in enumerate(s, start=1):
        indexed.append(pair)

    indexed1: list = []
    for pair in enumerate({100, 200, 300}, start=1):
        indexed1.append(pair)

    EXPECT_EQ(len(indexed), 3)
    EXPECT_EQ(indexed, indexed1)


@test("SetBuiltin", "ZipFunction")
def test_zip_function():
    """测试 zip 函数"""
    s1: set[int] = {1, 2, 3}
    s2: set[str] = {"a", "b", "c"}
    pairs: list = []

    # 现在 zip 支持直接迭代 set
    for pair in zip(s1, s2):
        pairs.append(pair)

    EXPECT_EQ(len(pairs), 3)


# ============================================================================
# 字面值常量测试
# ============================================================================


@test("SetBuiltin", "LiteralAsArgument")
def test_literal_as_argument():
    """测试字面值作为函数参数"""
    result: int = len({1, 2, 3, 4, 5})
    EXPECT_EQ(result, 5)

    minimum: int = min({5, 2, 8, 1, 9})
    EXPECT_EQ(minimum, 1)


@test("SetBuiltin", "LiteralInCondition")
def test_literal_in_condition():
    """测试字面值在条件表达式中"""
    value: int = 5 if 3 in {1, 2, 3} else 10
    EXPECT_EQ(value, 5)

    # 使用 bool() 显式转换
    value2: int = 5 if bool({1, 2}) else 10
    EXPECT_EQ(value2, 5)

    # 空 set 的布尔值
    empty: set[int] = set()
    value3: int = 5 if bool(empty) else 10
    EXPECT_EQ(value3, 10)


@test("SetBuiltin", "EmptyLiteral")
def test_empty_literal():
    """测试空集合常量"""
    empty1: set[int] = set()
    empty2: set = set()

    EXPECT_EQ(len(empty1), 0)
    EXPECT_EQ(len(empty2), 0)
    EXPECT_FALSE(bool(empty1))
    EXPECT_FALSE(bool(empty2))


# ============================================================================
# 字符串表示测试（暂时跳过，等待 str() 函数支持）
# ============================================================================
# 注：str() 和 repr() 函数尚未实现


# ============================================================================
# 高级推导式测试
# ============================================================================


@test("SetBuiltin", "NestedComprehension")
def test_nested_comprehension():
    """测试嵌套集合推导式"""
    matrix: list[list[int]] = [[1, 2], [3, 4], [5, 6]]
    all_nums: set[int] = {num for row in matrix for num in row}

    EXPECT_EQ(len(all_nums), 6)
    EXPECT_TRUE(1 in all_nums)
    EXPECT_TRUE(6 in all_nums)


@test("SetBuiltin", "ComprehensionFromLiteral")
def test_comprehension_from_literal():
    """测试从字面值列表的推导式"""
    evens: set[int] = {x for x in [1, 2, 3, 4, 5, 6] if x % 2 == 0}
    EXPECT_EQ(len(evens), 3)
    EXPECT_TRUE(2 in evens)
    EXPECT_TRUE(4 in evens)
    EXPECT_TRUE(6 in evens)


# ============================================================================
# 循环控制测试
# ============================================================================


@test("SetBuiltin", "ForLoopWithBreak")
def test_for_loop_with_break():
    """测试 for 循环中的 break"""
    s: set[int] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    count = 0

    for num in s:
        count = count + 1
        if count == 5:
            break

    EXPECT_EQ(count, 5)


@test("SetBuiltin", "ForLoopWithContinue")
def test_for_loop_with_continue():
    """测试 for 循环中的 continue"""

    odd_sum = 0
    for num in {1, 2, 3, 4, 5}:
        if num % 2 == 0:
            continue
        odd_sum = odd_sum + num

    EXPECT_TRUE(odd_sum > 0)


# ============================================================================
# 引用语义测试
# ============================================================================


@test("SetBuiltin", "AssignmentSharing")
def test_assignment_sharing():
    """测试赋值共享引用"""
    s1: set[int] = {1, 2, 3}
    s2: set[int] = s1

    # 通过 s2 修改会影响 s1
    s2.add(4)
    EXPECT_EQ(len(s1), 4)
    EXPECT_TRUE(4 in s1)

    # 通过 s1 修改会影响 s2
    s1.add(5)
    EXPECT_EQ(len(s2), 5)
    EXPECT_TRUE(5 in s2)


@test("SetBuiltin", "ReferenceInDict")
def test_reference_in_dict():
    """测试 dict 中的 set 引用"""
    original: set[int] = {1, 2, 3}
    data: dict = {"my_set": original}

    # 修改原始 set
    original.add(4)

    # dict 中的 set 也会变化（引用语义）
    stored: set = data["my_set"]
    EXPECT_EQ(len(stored), 4)
    EXPECT_TRUE(4 in stored)

    stored.add(5)
    EXPECT_EQ(len(original), 5)
    EXPECT_TRUE(5 in original)


@test("SetBuiltin", "AnySetReference")
def test_any_set():
    """测试 set 的弱类型引用赋值行为

    注意：与 list 不同，set 在 C++ 侧统一使用 mcpy::variant_set（弱类型）。
    因此 set[int] 和 set[Any] 的赋值是引用赋值，不会触发类型转换。

    这与 list 的行为不同：
    - list[Any] → list[int] 会构造新的 mc::array<int> 并逐元素转换
    - set[Any] → set[int] 只是引用赋值，共享同一个 variant_set

    设计理由：list 支持强类型和弱类型是因为与三方 C/C++ 库对接经常需要连续元素内存，
    而 set/dict 是更高级数据结构没有这种要求，使用弱类型保持与 python 语义一致。

    注意：弱类型转换成强类型会影响代码生成，因为类型推导引擎会根据强类型生成后续的代码，
    这可能会引发类型转换失败的运行期异常。因此需要谨慎使用弱类型转换成强类型。
    """
    original: set[Any] = {1, "str", 3}

    # 引用赋值，stored 和 original 指向同一个 variant_set
    stored: set[int] = original
    EXPECT_EQ(len(stored), 3)

    # stored 仍然包含 "str"，因为没有类型转换
    EXPECT_TRUE("str" in stored)

    # 修改 stored 会影响 original（证明是引用）
    stored.add(4)
    EXPECT_EQ(len(original), 4)
    EXPECT_TRUE(4 in original)


# ============================================================================
# 其他内置函数测试（暂时跳过）
# ============================================================================
# 注：filter() 函数和 lambda 表达式尚未完全支持
